package org.example.pet.common;

import java.io.Serializable;

/**
 * 通用返回结果
 * @param <T>
 */
public class Result<T> implements Serializable {
    private Integer code;   // 1为成功，0为失败
    private String msg; // 错误信息
    private T data; // 响应数据

    public static<T> Result<T> success(String msg) {
        Result<T> res = new Result<>();
        res.setCode(1);
        res.setMsg(msg);
        return res;
    }
    public static<T> Result<T> success(T data) {
        Result<T> res = new Result<>();
        res.setCode(1);
        res.setData(data);
        return res;
    }
    
    public static<T> Result<T> success(String msg, T data) {
        Result<T> res = new Result<>();
        res.setCode(1);
        res.setMsg(msg);
        res.setData(data);
        return res;
    }
    
    public static<T> Result<T> err(String errMsg) {
        Result<T> res = new Result<>();
        res.setCode(0);
        res.setMsg(errMsg);
        return res;
    }

    public static<T> Result<T> err(Integer code, String errMsg) {
        Result<T> res = new Result<>();
        res.setCode(code);
        res.setMsg(errMsg);
        return res;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
